//
//  GoodsListCell.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/10/8.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "GoodsListCell.h"

@implementation GoodsListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setMainCell];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        //self.contentView.backgroundColor = [UIColor orangeColor];
    }
    return self;
}

- (void)setMainCell
{
    self.topTitleLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.topTitleLabel];
    self.topTitleLabel.textAlignment = 0;
    self.topTitleLabel.font = FontWithSize(14);
    self.topTitleLabel.textColor = BlackTextColor;
    self.topTitleLabel.numberOfLines = 0;
    self.topTitleLabel.userInteractionEnabled = YES;
    
    self.topTiteLabelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.topTitleLabel addSubview:self.topTiteLabelBtn];
    
    self.topPriceLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.topPriceLabel];
    self.topPriceLabel.textAlignment = 2;
    self.topPriceLabel.font = FontWithSize(14);
    self.topPriceLabel.textColor = RGBACOLOR(153, 153, 153, 1);
    
    self.priceBgView = [[UIView alloc] init];
    [self.contentView addSubview:self.priceBgView];
    self.priceBgView.backgroundColor = GrayTextColor;
    
    self.priceLabel = [[UILabel alloc] init];
    [self.priceBgView addSubview:self.priceLabel];
    self.priceLabel.textColor = BlackTextColor;
    self.priceLabel.font = FontWithSize(12);
    
    self.earningsLabel = [[UILabel alloc] init];
    [self.priceBgView addSubview:self.earningsLabel];
    self.earningsLabel.textColor = BlackTextColor;
    self.earningsLabel.font = FontWithSize(12);
    self.earningsLabel.textAlignment = 2;
    
    self.leftImage = [[UIImageView alloc]init];
    [self.contentView addSubview:self.leftImage];
    self.leftImage.contentMode = UIViewContentModeScaleAspectFill;
    self.leftImage.clipsToBounds = YES;
    self.leftImage.userInteractionEnabled = YES;
    
    self.leftImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftImage addSubview:self.leftImageBtn];
    
    self.rightImgae = [[UIImageView alloc]init];
    [self.contentView addSubview:self.rightImgae];
    self.rightImgae.contentMode = UIViewContentModeScaleAspectFill;
    self.rightImgae.clipsToBounds = YES;
    self.rightImgae.userInteractionEnabled = YES;
    
    self.rightImgaeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightImgae addSubview:self.rightImgaeBtn];
    
    self.shanrkBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.shanrkBtn];
    [self.shanrkBtn setTitle:@"分享" forState:UIControlStateNormal];
    [self.shanrkBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    self.shanrkBtn.titleLabel.font = FontWithSize(12);
    self.shanrkBtn.backgroundColor = BtnBgColor;
    self.shanrkBtn.layer.cornerRadius = 15;
    self.shanrkBtn.layer.masksToBounds = YES;
    
    self.changePriceBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.changePriceBtn];
    [self.changePriceBtn setTitle:@"改价" forState:UIControlStateNormal];
    [self.changePriceBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    self.changePriceBtn.titleLabel.font = FontWithSize(12);
    self.changePriceBtn.backgroundColor = [UIColor whiteColor];
    self.changePriceBtn.layer.cornerRadius = 15;
    self.changePriceBtn.layer.masksToBounds = YES;
    self.changePriceBtn.layer.borderWidth = 1;
    self.changePriceBtn.layer.borderColor = BtnBgColor.CGColor;
    
    self.recommendBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.recommendBtn];
    [self.recommendBtn setTitle:@"推荐精选" forState:UIControlStateNormal];
    [self.recommendBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    self.recommendBtn.titleLabel.font = FontWithSize(12);
    self.recommendBtn.backgroundColor = BtnBgColor;
    self.recommendBtn.layer.cornerRadius = 15;
    self.recommendBtn.layer.masksToBounds = YES;
   
    
    self.doneMecommend = [[UILabel alloc] init];
    [self.contentView addSubview:self.doneMecommend];
    self.doneMecommend.text = @"已推荐";
    self.doneMecommend.textColor = BlackTextColor;
    self.doneMecommend.textAlignment = 0;
    self.doneMecommend.font = FontWithSize(12);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.topTitleLabel.frame = CGRectMake(15, 10, self.width-30, 40);
    self.topTiteLabelBtn.frame = CGRectMake(0, 0, self.topTitleLabel.width, self.topTitleLabel.height);
    
    self.topPriceLabel.frame = CGRectMake(SCREENWIDTH-15-200, self.topTitleLabel.bottom+5, 200, 14);
    
    self.priceBgView.frame = CGRectMake(15, self.topPriceLabel.bottom+5, self.topTitleLabel.width, 34);
    self.priceLabel.frame = CGRectMake(10, 11, 150, 12);
    self.earningsLabel.frame = CGRectMake(self.priceBgView.width-150-10, 11, 150, 12);
    
    CGFloat width = (SCREENWIDTH-30-7)/2;
    self.leftImage.frame = CGRectMake(15, self.priceBgView.bottom+10, width, width);
    self.rightImgae.frame = CGRectMake(self.leftImage.right+7, self.priceBgView.bottom+10, width, width);
    self.leftImage.clipsToBounds = YES;
    self.rightImgae.clipsToBounds = YES;
    
    self.leftImageBtn.frame = CGRectMake(0, 0, width, width);
    self.rightImgaeBtn.frame = CGRectMake(0, 0, width, width);
    
    self.shanrkBtn.frame = CGRectMake(self.width-70-15, self.leftImage.bottom+20, 70, 30);
    
    self.changePriceBtn.frame = CGRectMake(self.shanrkBtn.left-70-10, self.leftImage.bottom+20, 70, 30);
    
    self.recommendBtn.frame = CGRectMake(self.changePriceBtn.left-70-10, self.leftImage.bottom+20, 70, 30);
    
    self.doneMecommend.frame = CGRectMake(15, self.leftImage.bottom + 29, 100, 12);
}

- (void)setCellModel:(MainModel *)model
{
    self.topTitleLabel.text = [NSString stringWithFormat:@"%@",model.title];
    self.topPriceLabel.text = [NSString stringWithFormat:@"店主成本价：￥%0.2f",[model.shopCostPrice floatValue]];
    self.priceLabel.text = [NSString stringWithFormat:@"店主销售价：￥%0.2f",[model.price floatValue]];
    CGFloat earnings = [model.price floatValue] - [model.shopCostPrice floatValue];
    self.earningsLabel.text = [NSString stringWithFormat:@"预计收益：￥%0.2f",earnings];
   
    self.leftImage.hidden = YES;
    self.rightImgae.hidden = YES;
    
    NSArray *imageArray = model.imageUrlList;
    for (int i = 0; i < imageArray.count; i++)
    {
        switch (i)
        {
            case 0:
                self.leftImage.hidden = NO;
                [self.leftImage sd_setImageWithURL:[NSURL URLWithString:imageArray[i]] placeholderImage:[UIImage imageNamed:@"zhanwei100"]];
                break;
                
            case 1:
                self.rightImgae.hidden = NO;
                [self.rightImgae sd_setImageWithURL:[NSURL URLWithString:imageArray[i]] placeholderImage:[UIImage imageNamed:@"zhanwei100"]];
                break;
                
            default:
                break;
        }
    }
    
    if ([model.isShopRecommend boolValue])
    {//推荐
        self.recommendBtn.hidden = YES;
        self.doneMecommend.hidden = NO;
    }
    else {//未推荐
        self.recommendBtn.hidden = NO;
        self.doneMecommend.hidden = YES;
    }
    
}




@end
